<html>
<head><meta charset="utf-8"><title>GAT Constraints · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html">GAT Constraints</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="246286786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246286786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246286786">(Jul 16 2021 at 22:12)</a>:</h4>
<p>Has anyone used GATs in RA? I reported <a href="https://github.com/rust-analyzer/rust-analyzer/issues/9602">#9602</a> after constraints gave me parsing failures, but I'm curious if anyone knows another way to specify a concrete value for a GAT-trait's associated type that might be a more-RA-friendly workaround?</p>
<p><a href="user_uploads/4715/tQuTYb4elgRFJgj57SXw9QeV/image.png">image.png</a></p>
<div class="message_inline_image"><a href="user_uploads/4715/tQuTYb4elgRFJgj57SXw9QeV/image.png" title="image.png"><img src="user_uploads/4715/tQuTYb4elgRFJgj57SXw9QeV/image.png"></a></div>



<a name="246287646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246287646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246287646">(Jul 16 2021 at 22:23)</a>:</h4>
<p>I don't think you can escape that problem with a different syntax, at least I don't think there is an alternative?<br>
The problem here is we just don't expect generics for the left of associated type args currently</p>



<a name="246287658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246287658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246287658">(Jul 16 2021 at 22:23)</a>:</h4>
<p>We only expect a simple nameref followed by a <code>=</code> token, <a href="https://github.com/rust-analyzer/ungrammar/blob/3207f958081ee1272ff88f0ff32fd302b5b5d78c/rust.ungram#L53-L54">https://github.com/rust-analyzer/ungrammar/blob/3207f958081ee1272ff88f0ff32fd302b5b5d78c/rust.ungram#L53-L54</a></p>



<a name="246292448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246292448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246292448">(Jul 16 2021 at 23:47)</a>:</h4>
<p>Hmm so I looked into this and I am not sure how we wanna fix this. Currently RA does a lookahead of 1 to check if the current token is an ident followed by a <code>=</code> or <code>:</code> and parse accordingly, if its neither of those it parses a type. With GATs this lookahead wont work anymore as there can be an infinite amount of tokens inbetween...</p>



<a name="246292517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246292517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246292517">(Jul 16 2021 at 23:49)</a>:</h4>
<p>rustc solves this by just parsing a type(generic arg), then trying to extract the ident and args out of that, which isnt something we can do either. <a href="https://github.com/rust-lang/rust/blob/74ef0c3e404cc72c08b2d1e14506f90d9e877269/compiler/rustc_parse/src/parser/path.rs#L440-L443">https://github.com/rust-lang/rust/blob/74ef0c3e404cc72c08b2d1e14506f90d9e877269/compiler/rustc_parse/src/parser/path.rs#L440-L443</a></p>



<a name="246292614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246292614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246292614">(Jul 16 2021 at 23:50)</a>:</h4>
<p>And if I see this right we cant just try and parse this as expecting a <code>=</code> and <code>:</code> and just reparse the consumed input as a type if we didnt find either of those without duplicating most path parsing logic? Though maybe I'm not thinking with the parser infra and markers of RA correctly yet</p>



<a name="246292991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246292991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246292991">(Jul 16 2021 at 23:57)</a>:</h4>
<p>Is there a way to maybe move into a n-lookahead context when this would otherwise error, and only accept types or up to N closing <code>&gt;</code>s before being _sure_ it's an invalid parse?</p>



<a name="246293335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293335">(Jul 17 2021 at 00:02)</a>:</h4>
<p>What about having something with N+1 instances though</p>



<a name="246293441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293441">(Jul 17 2021 at 00:04)</a>:</h4>
<p>Trying to rephrase... We know that there must be a matching close-<code>&gt;</code>, and we can have any number of <code>&lt;</code>/<code>&gt;</code> pairs- treat it like parathetical expressions; if we see enough closes to close all opens and we still can't parse the contents, it's a bad parse.</p>



<a name="246293462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293462">(Jul 17 2021 at 00:05)</a>:</h4>
<p>The problem isnt a bad parse, the problem is how to reparse.</p>



<a name="246293473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293473">(Jul 17 2021 at 00:05)</a>:</h4>
<p>Basically, we could parse an <code>NameRef</code>, then parse a <code>GenericArgList</code>, and then check if <code>=</code> and <code>:</code> follow. If those do not follow we cannot reparse what we have parsed into a <code>PathType</code> from my understanding of how the parser works, unless we duplicate the path parsing logic somewhat from my understanding. Though I'll take a look again if this can be fixed up with markers somehow</p>



<a name="246293658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293658">(Jul 17 2021 at 00:08)</a>:</h4>
<p>Oh- couldn't that just be another parser on Alternative?</p>
<div class="codehilite"><pre><span></span><code>GatConstraint =
  NameRef GenericArgList &#39;=&#39; ... | PathType
</code></pre></div>
<p>?</p>
<p>Or is generalized backtracking not an option?</p>



<a name="246293735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293735">(Jul 17 2021 at 00:10)</a>:</h4>
<p>Ye basically backtracking is the problem right now</p>



<a name="246293747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293747">(Jul 17 2021 at 00:10)</a>:</h4>
<p>specifying the grammar for this is simple</p>



<a name="246293751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293751">(Jul 17 2021 at 00:10)</a>:</h4>
<p>but the RA parser infra doesnt really support backtracking like this that well, I think</p>



<a name="246293770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293770">(Jul 17 2021 at 00:11)</a>:</h4>
<p>Or well it kinda does, though in this case its a bit tricky for me to figure out right now <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="246293785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293785">(Jul 17 2021 at 00:11)</a>:</h4>
<p>My apologies- I don't have much context outside of recursive descent parsing.</p>
<p>I suspect you can do state extraction in the parsers and map it to <code>Either</code> at the top-level parse? That mechanism is somewhat essential I'd imagine.</p>



<a name="246293990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246293990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246293990">(Jul 17 2021 at 00:15)</a>:</h4>
<p>No worries <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span><br>
Okay actually this isn't so tricky after all, it just requires a few changes at some other spots I think</p>



<a name="246294289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246294289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246294289">(Jul 17 2021 at 00:22)</a>:</h4>
<p><span aria-label="tada" class="emoji emoji-1f389" role="img" title="tada">:tada:</span> Awesome- if there's any additional info I can provide, just @ me :)</p>



<a name="246296957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246296957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246296957">(Jul 17 2021 at 01:33)</a>:</h4>
<p>Alright, I got it working I think</p>



<a name="246297180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246297180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246297180">(Jul 17 2021 at 01:38)</a>:</h4>
<p>Will clean that up tomorrow(if I can clean it up even) <a href="https://github.com/rust-analyzer/rust-analyzer/pull/9619">https://github.com/rust-analyzer/rust-analyzer/pull/9619</a></p>



<a name="246351400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/GAT%20Constraints/near/246351400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/GAT.20Constraints.html#246351400">(Jul 18 2021 at 00:22)</a>:</h4>
<p>For anyone familiar with parsing and grammar inside of RA- <a href="https://github.com/rust-analyzer/rust-analyzer/pull/9619">the above PR</a> is marked as ready for review.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>